/*! \file image.h	
	This is the header file for image.cpp
	
	This contains the Image class
*/

#ifndef IMAGE_H
#define IMAGE_H

#include <GL/glfw.h>
#include <corona.h>
#include <set>
#include <map>
#include <fstream>
#include <iostream>
#include <cmath>

#include "game.h"

using namespace std;


/**
* \union Color
* This contains red, green, blue, and alpha values
*/
union Color {
	Color( float r = 0, float g = 0, float b = 0, float a = 1.0f );
	friend ostream& operator<<( ostream& o, Color& c );
	float array[4];
	struct {
		float r, g, b, a;
	};
};

ostream& operator<<( ostream& o, Color& c );

typedef struct Image {
	// X, Y, W, and H represent the rectangle that encompasses the image in the
	// image file on the hard drive
	int x, y, w, h;
	// Sort based on IDs
	//~ bool operator<( const Image& other ) const {
		//~ return id < other.id;
	//~ }
	
	Image( int x, int y, int w, int h );
	//~ void toString() { 
		//~ cout << "X: " << x << " y: " << y << " w: " << w << " h: " << h << endl;
	//~ }
};

//~ // Sort Image* based on IDs
//~ class ImageCompare {
//~ public:
	//~ bool operator()( Image* lhs, Image* rhs ) {
		//~ return lhs->id == rhs->id;
	//~ }
//~ };

typedef struct TextureHandler {
	// This holds the rectangles corresponding to selectable images in the texture
	map<int, Image*> images;
	TextureHandler( int w, int h );
	// These are the properties of the texture itself
	int w, h;
};

typedef struct GLImage {
    int w, h;
};
// ImageHandler has a map whose key is a textureID and whose value is a TextureHandler
// the TextureHandler has a map whose key is the imageID and whose value is the image rectangle

/**
	\class ImageHandler

*/
class ImageHandler {
private:
	static ImageHandler* imageHandler;
	map<int, TextureHandler*> textures;
	int boundTexture;
	int w, h; // of the bound texture

    ImageHandler( );

	/**
	* The ImageHandler should have an image file somewhere with all of the background images, border
	* images, etc. The workingTextureID specifies the the texture ID of that, according to OpenGL
	*/
	int workingTextureID; 
public: 
	static ImageHandler* getInstance();

    int loadImage(char* fname, char* rectFile = "SAME", GLuint filter = GL_LINEAR );
   
	// Send the ID of the graphic
    void blit(int textureID, int imageID, float angle = 9999.0f, int overrideW = -1, int overrideH = -1 );
	
	void guiBorder( int sideID, int cornerID, int x, int y, int w, int h );
	void guiBorder( int sideID, int cornerID, int w, int h );

	void getImageSize( int &w, int &h, int imageID );
		
	
	/**
	* Basic mutator for workingTextureID
	*/
	void setWorkingTexture( int id );
	
	/**
	* Basic accessor for workingTextureID
	*/
	int getWorkingTexture();
	
};


#endif
